package com.blank.task19;

import java.io.*;

public class FileCopyTest {
    public static void fileCopy(String oldPath,String newPath){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1、构造BufferedInputSteam类型的对象与形参oldPath文件关联
            bis = new BufferedInputStream(new FileInputStream(oldPath));
            //2、构造BufferedInputSteam类型的对象与形参newPath文件关联
            bos = new BufferedOutputStream(new FileOutputStream(newPath));

            //3、不断从输入流中读取数据到输出流中，即copy
            System.out.println("正在进行copy中...");

            byte[] bArr = new byte[1024];
            int res = 0;
            while((res = bis.read(bArr)) != -1){
                bos.write(bArr,0,res);
            }
            System.out.println("copy完成...");

            //4、关闭流对象并释放相关资源
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != bis){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != bos){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
